﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using DG.Tweening;
/*
 *属性展示栏右滑关闭
 */
public class jarodInputController : MonoBehaviour
{
    public GameObject attributeButton;//属性按钮

    public Transform diyizhang;//第一章
    public Transform dierzhang;//第二章
    public GameObject xiangshanghua;//向上滑
    public GameObject xiangxiahua;//向下滑

    private float fingerActionSensitivity = Screen.width * 0.05f; //手指动作的敏感度，这里设定为 二十分之一的屏幕宽度.
                                                                  
    private float fingerBeginX;
    private float fingerBeginY;
    private float fingerCurrentX;
    private float fingerCurrentY;
    private float fingerSegmentX;
    private float fingerSegmentY;
    //
    private int fingerTouchState;
    //
    private int FINGER_STATE_NULL = 0;
    private int FINGER_STATE_TOUCH = 1;
    private int FINGER_STATE_ADD = 2;

    float lastTime;
    Animator panelAni;
    // Use this for initialization
    void Start()
    {
        panelAni = this.gameObject.GetComponent<Animator>();
        fingerActionSensitivity = Screen.width * 0.05f;
        fingerBeginX = 0;
        fingerBeginY = 0;
        fingerCurrentX = 0;
        fingerCurrentY = 0;
        fingerSegmentX = 0;
        fingerSegmentY = 0;

        fingerTouchState = FINGER_STATE_NULL;
    }
    // Update is called once per frame
    void Update()
    {
        if(lastTime > 0)
        {
            lastTime -= 1 * Time.deltaTime;
        }
        

        if (Input.GetKeyDown(KeyCode.Mouse0))
        {

            if (fingerTouchState == FINGER_STATE_NULL)
            {
                fingerTouchState = FINGER_STATE_TOUCH;
                fingerBeginX = Input.mousePosition.x;
                fingerBeginY = Input.mousePosition.y;
            }

        }

        if (fingerTouchState == FINGER_STATE_TOUCH)
        {
            fingerCurrentX = Input.mousePosition.x;
            fingerCurrentY = Input.mousePosition.y;
            fingerSegmentX = fingerCurrentX - fingerBeginX;
            fingerSegmentY = fingerCurrentY - fingerBeginY;

        }


        if (fingerTouchState == FINGER_STATE_TOUCH)
        {
            float fingerDistance = fingerSegmentX * fingerSegmentX + fingerSegmentY * fingerSegmentY;

            if (fingerDistance > (fingerActionSensitivity * fingerActionSensitivity))
            {
                toAddFingerAction();
            }
        }

        if (Input.GetKeyUp(KeyCode.Mouse0))
        {
            fingerTouchState = FINGER_STATE_NULL;
        }
    }

    private void toAddFingerAction()
    {

        fingerTouchState = FINGER_STATE_ADD;

        if (Mathf.Abs(fingerSegmentX) > Mathf.Abs(fingerSegmentY))
        {
            fingerSegmentY = 0;
        }
        else
        {
            fingerSegmentX = 0;
        }

        if (fingerSegmentX == 0)
        {
            if (fingerSegmentY > 0)
            {
                
                if(this.gameObject.name == "MainLineSlidingBtn" && lastTime <= 0 && VariableDocument.GetInstance.chapters == 1)
                {
                    lastTime = 0.5f;
                    //Debug.Log("up");
                    VariableDocument.GetInstance.chapters = 2;
                    diyizhang.DOLocalMoveY(1334, 0.1f*1334f * Time.deltaTime);
                    dierzhang.DOLocalMoveY(0, 0.1f*1334 * Time.deltaTime);
                    xiangshanghua.SetActive(false);
                    xiangxiahua.SetActive(true);
                }
            }
            else
            {
                if (this.gameObject.name == "MainLineSlidingBtn" && lastTime <= 0 && VariableDocument.GetInstance.chapters == 2)
                {
                    lastTime = 0.5f;
                    //Debug.Log("down");
                    VariableDocument.GetInstance.chapters = 1;
                    diyizhang.DOLocalMoveY(0, 0.1f * 1334f * Time.deltaTime);
                    dierzhang.DOLocalMoveY(-1334, 0.1f * 1334 * Time.deltaTime);
                    xiangshanghua.SetActive(true);
                    xiangxiahua.SetActive(false);
                }

            }
        }
        else if (fingerSegmentY == 0)
        {
            if (fingerSegmentX > 0)
            {
                //Debug.Log("right");
                if(this.gameObject.name == "attributePanel")
                {
                    panelAni.SetBool("go", false);
                    panelAni.SetBool("back", true);
                    attributeButton.SetActive(true);
                }
            }
            else
            {
                //Debug.Log("left");
            }
        }

    }
}